﻿/* License
 * 
 * The MIT License (MIT)
 *
 * Copyright (c) 2013, Sylvain PONTOREAU (pontoreau.sylvain@outlook.com / sylvain.pontoreau.com)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

using System;
using System.Linq;
using System.Reflection;
using Xlsx.ComponentModel;
using Xlsx.ComponentModel.DataAnnotations;
using Xlsx.ComponentModel.Enums;

namespace Xlsx.DataBinder.Extensions
{
    /// <summary>
    /// Property info extensions
    /// </summary>
    public static class PropertyInfoExtensions
    {
        /// <summary>
        /// Returns a value that indicates whether the property is XlsxBindable.
        /// </summary>
        /// <param name="propertyInfo">PropertyInfo to check.</param>
        /// <returns>true if the current property is XlsxBindable; false otherwise.</returns>        
        public static bool IsXlsxBindable(this PropertyInfo propertyInfo)
        {
            //if any of the attributes is XlsxNonBindableAttribute, return false
            return !propertyInfo.GetCustomAttributes(true).Any(x => x is XlsxNonBindableAttribute);
        }

        /// <summary>
        /// Get the XlsxBindableInfo of the property.
        /// </summary>
        /// <param name="propertyInfo">PropertyInfo.</param>
        /// <returns>XlsxBindableInfo of the property.</returns>
        public static XlsxBindableInfo GetXlsxBindableInfo(this PropertyInfo propertyInfo)
        {
            var propertyAttributes = propertyInfo.GetCustomAttributes(true);

            //check if property is bindable in first
            if (propertyAttributes.Any(x => x is XlsxNonBindableAttribute))
                return null;
            
            if (propertyAttributes.Any(x => x is XlsxBindablePropertyAttribute))
                //if the property have a XlsxBindableAttribute
            {
                var attrib =
                    (XlsxBindablePropertyAttribute)
                    propertyAttributes.Single(x => x is XlsxBindablePropertyAttribute);
                return (attrib.BindableInfo.Type != XlsxDataType.None) ? attrib.BindableInfo : null;
            }

            var propertyDataType = propertyInfo.GetXlsxDataType();

            if (propertyDataType != XlsxDataType.None)
            {
                return new XlsxBindableInfo
                    {
                        ColumnName = propertyInfo.Name,
                        Order = 0,
                        Type = propertyDataType
                    };
            }

            return null;
        }

        /// <summary>
        /// Determine the XlsxDataType.
        /// </summary>
        /// <param name="propertyInfo">PropertyInfo.</param>
        /// <returns>XlsxDataType of the property.</returns>
        public static XlsxDataType GetXlsxDataType(this PropertyInfo propertyInfo)
        {
            var typeCode = Type.GetTypeCode(propertyInfo.PropertyType);

            switch (typeCode)
            {
                case TypeCode.Boolean:
                    return XlsxDataType.Boolean;
                case TypeCode.DateTime:
                    return XlsxDataType.DateTime;
                case TypeCode.Object:
                    return XlsxDataType.Object;
                case TypeCode.String:
                    return XlsxDataType.String;
                case TypeCode.Empty:
                case TypeCode.DBNull:
                    return XlsxDataType.None;
                default:
                    return XlsxDataType.Numeric;
            }
        }
    }
}
